<template>
  <div class="teacher-list-container">
    <!--查询表单-->
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item>
        <el-input
          clearable
          maxlength="20"
          show-word-limit
          oninput="value=value.replace(/[^\u4E00-\u9FA5]/g, '');"
          v-model="searchParams.name"
          clearable placeholder="讲师名"/>
      </el-form-item>

      <el-form-item>
        <el-select v-model="searchParams.level" clearable placeholder="讲师头衔" style="width: 150px">
          <el-option :value="0" label="高级讲师"/>
          <el-option :value="1" label="首席讲师"/>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-select v-model="searchParams.status" clearable placeholder="讲师状态" style="width: 120px">
          <el-option :value="0" label="启用"/>
          <el-option :value="1" label="禁用"/>
        </el-select>
      </el-form-item>

      <el-form-item prop="date" label="添加时间：">
        <el-date-picker
          v-model="date"
          type="daterange"
          align="right"
          unlink-panels
          value-format="yyyy-MM-dd"
          range-separator="至 "
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :picker-options="pickerOptions">
        </el-date-picker>
      </el-form-item>

      <el-button type="primary" icon="el-icon-search" @click="fetchData()" size="medium">查询</el-button>
      <el-button type="warning" icon="el-icon-refresh" @click="resetData()" size="medium">清空</el-button>
    </el-form>
    <!--表格-->
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="数据加载中"
      border
      fit
      highlight-current-row>

      <el-table-column
        label="序号"
        width="70"
        align="center">
        <template slot-scope="scope">
          {{ (page - 1) * limit + scope.$index + 1 }}
        </template>
      </el-table-column>

      <el-table-column prop="name" label="姓名" width="100" align="center" />

      <el-table-column label="头衔" width="100" align="center">
        <template slot-scope="scope">
          {{ scope.row.level===0?'高级讲师':'首席讲师' }}
        </template>
      </el-table-column>

      <el-table-column prop="intro" label="资历" />

      <el-table-column prop="gmtCreate" label="添加时间" width="160" align="center"/>

      <el-table-column prop="status" label="是否启用" width="80">
        <template slot-scope="scope">
        <el-switch
          style="display: block"
          v-model="scope.row.status===0?true:false"
          active-color="#13ce66"
          inactive-color="#ff4949"
          @change="changeTeacherStatus(scope.row.teacherId,scope.row.status)">
        </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200" align="center">
        <template slot-scope="scope">
          <router-link :to="'/manage/teacher/edit/'+scope.row.teacherId">
            <el-button type="primary" size="mini" icon="el-icon-edit">修改</el-button>
          </router-link>
          <el-button type="danger" size="mini" icon="el-icon-delete" @click="removeDataById(scope.row.teacherId)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      :current-page="page"
      :page-size="limit"
      :total="total"
      style="padding: 30px 0; text-align: center;"
      layout="total, prev, pager, next, jumper"
      @current-change="fetchData"
    />

  </div>
</template>

<script>
  import { getPageList,deleteById,updateStatusById } from '@/api/teacher'

  export default {
    data(){
      return {
        listLoading: true, // 是否显示loading信息
        list: null, // 数据列表
        total: 0, // 总记录数
        page: 1, // 页码
        limit: 10, // 每页记录数
        searchParams: {},// 查询条件
        date: '', //日期
        pickerOptions: {
          //不可选择当天以后日期
          disabledDate(time) {
            return time.getTime() > Date.now() - 8.64e6;
          },

          shortcuts: [{
            text: '最近一周',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '最近一个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '最近三个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit('pick', [start, end]);
            }
          },]
        },
      }
    },
    created() {
      this.fetchData()
    },
    methods:{
      fetchData(page = 1){
        if (this.date){
          this.searchParams.begin = this.date[0]
          this.searchParams.end = this.date[1]
        }
        //当点击分页组件的切换按钮的时候，会传输一个当前页码的参数page
        this.page = page
        this.listLoading = true
        getPageList(this.page, this.limit, this.searchParams)
        .then(result => {
          if (result.success == true){
            this.list = result.data.rows
            this.total = result.data.total
          }
          this.listLoading = false
        })
      },

      resetData() {
        this.date = ''
        this.searchParams = {}
        this.fetchData()
      },

      removeDataById(id) {
        this.$confirm('此操作将永久删除该记录, 是否继续？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          return deleteById(id)
        }).then(result => { // 如果上一个then成功则执行此处的then回调
          if (result.code === 20000){
            this.fetchData(this.page)
            this.$message({
              type: 'success',
              message: '删除成功！'
            })
          }
        }).catch((response) => {
          if (response === 'cancel') {
            this.$message({
              type: 'info',
              message: '已取消删除！'
            })
          }
        })
      },
      changeTeacherStatus(id,status){
        if (status == 1){
          status = 0
        }
        else status = 1
        this.$confirm('此操作将启用 / 禁用该讲师，是否继续？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          return updateStatusById(id,status)
        })
        .then(result => {
          if (result.success == true){
            this.fetchData(this.page)
            this.$message({
              type: 'success',
              message: '修改成功！'
            })
          }
        }).catch((response) => {
          if (response === 'cancel') {
            this.$message({
              type: 'info',
              message: '已取消修改！'
            })
          }
        })
      }
    }
  }
</script>

<style scoped>
  .teacher-list-container{
    margin-top: 20px;
    margin-left: 15px;
  }
  .el-switch{
    margin-left: 10px;
  }
</style>
